﻿using System;
using System.Linq;
using System.Windows.Forms;
using PersonalAccoutingProgram.Data;

namespace PersonalAccoutingProgram.View
{
    public partial class EditExpensesForm : Form
    {
        private int expenseID;
        private string date;
        private string amount;
        private string category;
        private ErrorProvider errorProvider;
        public EditExpensesForm(int expenseID, string date, 
                                string amount, string category)
        {
            InitializeComponent();
            
            this.date = date;
            dateTimePicker.Text = date;
            this.category = category;
            comboCategory.Text = category;
            this.amount = amount; 
            errorProvider = new ErrorProvider();
            InitializeControls(expenseID);
        }

        private void InitializeControls(int expenseID)
        {
            dateTimePicker.Format = DateTimePickerFormat.Custom;
            dateTimePicker.CustomFormat = DateFormat.DATE_TIME_FORMAT;           
            tbAmount.Text = this.amount;
            this.expenseID = expenseID;
            InitializeCategory();
        }

        private void InitializeCategory()
        {
            var allCategories = 
            CategoryDataClassesDataContext.GetAllCategories();
            comboCategory.Items.AddRange(allCategories.ToArray());
        }
        
        private void EditForm_Load(object sender, EventArgs e)
        {
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        { 
            try
            {
                if (DataValidator.isAmountValid(tbAmount.Text))
                { 
                    var categoryID = 
                    CategoryDataClassesDataContext.GetCategoryID(
                        comboCategory.Text);
                    decimal amountValue = decimal.Parse(tbAmount.Text);                  
                    int categoryIntID = categoryID.First();
                    DateTime date = DateTime.Parse(dateTimePicker.Text);
                    ExpenseDataClassesDataContext.UpdateExpenses(
                        expenseID, categoryIntID, amountValue, date);
                    errorProvider.SetError(tbAmount, ""); 
                    this.Close();
                }
            }
            catch (ArithmeticException)
            {
                errorProvider.SetError(tbAmount, "Too large amount!");            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ExpenseDataClassesDataContext.DeleteExpenses(expenseID);
            this.Close();
        }

        private void tbAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            DataValidator.ValidateAmount(e);
        }

        private void tbAmount_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {            
            try
            {
                decimal amount = decimal.Parse(tbAmount.Text);
                errorProvider.SetError(tbAmount,"");
            }
            catch (ArithmeticException)
            {
                errorProvider.SetError(tbAmount, "Too large amount!");
                e.Cancel = true;
            }
        }
    }
}